package com.ithema.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {
    //加密密钥
    private static final String SALT = "lolish";

    /**
     * 接收业务数据，生成token并返回
     */
    public static String getToken(Map<String, Object> claims) {
        return JWT.create()
                .withClaim("user", claims)
                .withExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 7))
                .sign(Algorithm.HMAC256(SALT));
    }

    /**
     * 接收token，验证token，并返回
     */
    public static Map<String, Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(SALT))
                .build()
                .verify(token)
                .getClaim("user")
                .asMap();
    }
}
